package com.shierke.Ume.ui.fragment.message

import ViewPagerAdapterLazyFragment
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.shierke.Ume.R
import com.shierke.Ume.ui.view.HXLinePagerIndicator
import com.shierke.Ume.util.click
import kotlinx.android.synthetic.main.fragment_message.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView

/**
 * Create by Ybx
 * on 2020-10-12
 * Desribe:
 */
class MessageFragment : ViewPagerAdapterLazyFragment() {
    override fun onFragmentShow() {

    }

    override fun onFragmentHide() {
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = LayoutInflater.from(activity).inflate(R.layout.fragment_message, null, false)
        return view
    }


    private var titles = arrayListOf<String>()
    private var fragments = arrayListOf<Fragment>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        titles.add(getString(R.string.msg_fragment_msg))
        titles.add(getString(R.string.msg_fragment_match))

        fragments.add(MessageListFragment())
        fragments.add(MessageMatchListFragment())


        val viewpagerAdapter = ViewpagerAdapter(childFragmentManager)

        messageViewPager.adapter = viewpagerAdapter


        var commonNavigator = CommonNavigator(activity)
        //commonNavigator.isAdjustMode = true//设置title宽度自适应
        commonNavigator.isEnablePivotScroll = true

        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val clipPagerTitleView = ClipPagerTitleView(context)
                clipPagerTitleView.text = titles[index]
                clipPagerTitleView.textSize = UIUtil.dip2px(context, 18.0).toFloat()
                clipPagerTitleView.textColor = Color.parseColor("#9C9C9C")
                clipPagerTitleView.clipColor = Color.parseColor("#2C2C2C")


                clipPagerTitleView.click {

                    if (messageViewPager != null) {
                        messageViewPager.currentItem = index
                    }
                }


                return clipPagerTitleView

            }

            override fun getCount() = titles.size

            override fun getIndicator(context: Context?): IPagerIndicator? {

                val indicator = HXLinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 6.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 40.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                return indicator
            }
        }

        indicator.navigator = commonNavigator
        ViewPagerHelper.bind(indicator, messageViewPager)

    }


    inner class ViewpagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getCount() = fragments.size

        override fun getItem(position: Int) = fragments[position]

        override fun getPageTitle(position: Int): CharSequence? {
            return titles[position]
        }
    }


}